<?php
namespace app\controller\api;

use app\exception\ErrnoPlus;
use app\service\DeviceGroupTableService;
use app\validate\DeviceGroupTableValidate;
use share\controller\ApiBaseController;
use share\exception\ApiException;
use share\service\ContextService;

class DeviceGroupTableController extends ApiBaseController{

    protected $beforeActionList = [
        "checkSign",
        "checkUser", 
    ];

    /**
     * @description: 
     * @return {*}
     * @use: 
     */
    public function initValidate(){
        $this->validate = new DeviceGroupTableValidate();
    }

    /**
     * 获取设备分组信息
     */
    public function list_action(){
        $input = $this->check();
        $userInfo = ContextService::getUserInfo();
        $res = DeviceGroupTableService::getListGroupInfo($input,$userInfo['id']);
        if(empty($res)){
            return self::fail("no data",ErrnoPlus::NOT_BACK_DATA);
        }
        return self::ok($res);
    }

    /**
     * 设置设备分组
     */
    public function set_action(){
        $input = $this->check();
        if($input['chanNum'] != count($input["deviceList"])){
            throw new ApiException("chanNum or deviceList num error",ErrnoPlus::COMMON_ERROR);
        }
        if($input['chanNum'] > 0){
            foreach($input["deviceList"] as $deviceList){
                $this->check('deviceListDetail',$deviceList);
            }
        }
        $userInfo = ContextService::getUserInfo();
        $uid = $userInfo['id'];
        if(isset($input['groupId']) && $input['groupId']){
            $res = DeviceGroupTableService::updateGroupInfo($input['groupId'],$input,$uid);
        }
        else{
            $res = DeviceGroupTableService::addGroupInfo($input,$uid);
        }
        return self::ok($res);
    }

    /**
     * 删除分组
     */
    public function del_action(){
        $input = $this->check();
        $userInfo = ContextService::getUserInfo();
        $uid = $userInfo['id'];
        $res = DeviceGroupTableService::delGroupInfo($input,$uid);
        return self::ok($res);
    }

}